Skip to content

Conversation

JustasMonkev
Copy link
Contributor

Checks for existing keys before calling Object.defineProperty
to avoid “Cannot redefine property” errors when the module is
evaluated multiple times or multiple SoftAssertionService
instances run in the same process.

This makes expect.soft and its utility methods idempotent and prevents startup crashes.

@Copilot Copilot AI review requested due to automatic review settings July 6, 2025 09:17
Copilot

This comment was marked as outdated.

@JustasMonkev JustasMonkev requested a review from Copilot July 6, 2025 09:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds idempotent initialization for soft assertion helpers on the expect object by checking for existing properties before defining them.

  • Introduces a SoftHelpers type and helperFactories object to centralize soft assertion methods.
  • Replaces repeated Object.defineProperty calls with a loop that guards against redefinition.
  • Updates the expectWithSoft cast to include SoftHelpers.
Comments suppressed due to low confidence (1)

src/index.ts:57

  • Consider adding a unit test that re-invokes this setup logic and asserts no errors are thrown and that helpers remain defined, to verify idempotency.
for (const [name, fn] of Object.entries(helperFactories)) {

@christian-bromann
Copy link
Member

@JustasMonkev thanks for raising the PR!

I've been working on webdriverio/webdriverio#14592 to get this fixed. The problem is that we shouldn't load expect-webdriverio multiple times within the same worker process. This causes side effects where multiple singleton services from this library are added to the testrunner. I've considered this change before I realised that the origin of the problem is the duplication of multiple expect-webdriverio instances loaded.

I will keep this PR open for now but above PR should fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants